# +----------------------------------------------------------------------
# | ChatWork
# +----------------------------------------------------------------------
# | 这不是一个自由软件,您只能在不用于商业目的的前提下对程序代码进行修改和使用。
# | 任何企业和个人不允许对程序代码以任何形式任何目的再发布,商业使用请获取授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
from typing import List, Dict, Union
from fastapi import Query
from pydantic import BaseModel, Field


class ChatHistorySearchIn(BaseModel):
    """ 对话历史搜索参数 """
    sid: int = Query(default=0, ge=0, description="会话ID")
    bot: str = Query(default="", max_length=32, description="智能体编号")
    scene: str = Query(default="normal", pattern=r"^(normal|test|web|api)$", description="来源场景")

    class Config:
        json_schema_extra = {
            "example": {
                "sid": 1,
                "bot": "HCxiS50f0d4nd",
                "scene": "normal"
            }
        }


class ChatDialogClearIn(BaseModel):
    """ 清空上下文参数 """
    sid: int = Field(default=0, ge=0, description="会话ID")
    bot: str = Field(default="", max_length=32, description="智能体编号")
    scene: str = Field(default="normal", pattern=r"^(normal|test|web|api)$", description="来源场景")

    class Config:
        json_schema_extra = {
            "example": {
                "sid": 1,
                "bot": "HCxiS50f0d4nd",
                "scene": "normal"
            }
        }


class ChatDialogSendIn(BaseModel):
    """ 发起对话参数 """
    sid: int = Field(default=0, description="会话ID")
    bot: str = Field(default="", max_length=32, description="智能体编号")
    scene: str = Field(default="normal", pattern=r"^(normal|test|web|api)$", description="来源场景")
    stream: bool = Field(default=True, description="流式响应")
    search: bool = Field(default=False, description="联网搜索")
    messages: list = Field(..., description="消息列表")

    class Config:
        json_schema_extra = {
            "example": {
                "sid": 1,
                "bot": "HCxiS50f0d4nd",
                "scene": "normal",
                "stream": True,
                "messages": [
                    {"role": "system", "content": "You are a helpful assistant."},
                    {"role": "user", "content": "Hello!"}
                ]
            }
        }


"""--------------- Separator ---------------"""


class ChatHistoryListVo(BaseModel):
    """ 对话历史列表Vo """
    class Question(BaseModel):
        id: int = Field(description="ID")
        type: str = Field(description="类型")
        avatar: str = Field(description="头像")
        content: str = Field(description="问题")
        create_time: str = Field(description="创建时间")
        attach: list = Field(description="附件文件")

        class Config:
            json_schema_extra = {
                "example": {
                    "id": 1,
                    "type": "question",
                    "avatar": "https://www.wa.com/storage/image/41049f9.png",
                    "content": "1+1等于多少",
                    "create_time": "2024-11-23 23:41:31",
                    "attach": [
                        {
                            "id": "5199f73ea1cf409abf0174cae90683d9",
                            "file_type": "image",
                            "file_name": "dou.png",
                            "file_size": 690191,
                            "file_ext": "png",
                            "file_path": "https://xx.com/storage/image/adb4bcfc0.png"
                        }
                    ]
                }
            }

    class Answer(BaseModel):
        id: int = Field(description="ID")
        type: str = Field(description="类型")
        avatar: str = Field(description="头像")
        thinks: str = Field(description="思考")
        content: str = Field(description="答复")
        quotes: list = Field(description="引用内容")
        context: list = Field(description="答复内容")
        annex: list = Field(description="答复附件")
        issues: List[str] = Field(description="猜您想问")
        tools_list: List[dict] = Field(description="MCP工具")
        web_search: List[dict] = Field(description="联网搜索")
        chat_model: str = Field(description="对话模型")
        end_clear: bool = Field(description="清除节点")
        is_clear: bool = Field(description="是否清除")
        task_time: str = Field(description="执行耗时")
        create_time: str = Field(description="创建时间")

        class Config:
            json_schema_extra = {
                "example":  {
                    "id": 1,
                    "type": "answer",
                    "avatar": "https://www.wa.com/storage/image/robot.png",
                    "thinks": "思考过程",
                    "content": "1+1等于2。",
                    "quotes": [
                        {
                            "uuid": "0bdbaa93-7905-4ae6-a220-79c8f592cabe",
                            "archive_id": 1,
                            "question": "embedding",
                            "answer": "ask",
                            "score": 1
                        }
                    ],
                    "context": [
                        {
                            "role": "Human",
                            "content": "1+1等于多少"
                        }
                    ],
                    "annex": [
                        {
                            "id": "5199f73ea1cf409abf0174cae90683d9",
                            "file_type": "image",
                            "file_name": "dou.png",
                            "file_size": 690191,
                            "file_ext": "png",
                            "file_path": "https://xx.com/storage/image/adb4bcfc0.png"
                        }
                    ],
                    "issues": ["什么是Python?", "Python的能力"],
                    "tools_list": [
                        {
                            "id": "id",
                            "code": "",
                            "name": "百度搜索 / AISearch",
                            "tool_name": "AISearch",
                            "tool_logo": "https://www.x.cn/aa.png",
                            "arguments": "{'query': 'python'}",
                            "response": "{'content': {'type': 'text', 'text': '...'}}"
                        }
                    ],
                    "web_search": [
                        {
                            "title": "东方财富财经早餐4月10日周四",
                            "link": "",
                            "icon": "",
                            "media": "",
                            "refer": "ref_1",
                            "content": "特朗普称已授权对不采取报复行动"
                        }
                    ],
                    "chat_model": "GLM-4-Long",
                    "end_clear": False,
                    "is_clear": False,
                    "task_time": "1.05s",
                    "create_time": "2024-11-23 23:41:31"
                }
            }


class ChatPublishVerifyVo(BaseModel):
    """ 对话分布校验Vo """
    code: str = Field(description="智能体编号")
    logo: str = Field(description="智能体图标")
    name: str = Field(description="智能体名称")
    intro: str = Field(description="智能体简介")
    welcome_text: str = Field(description="欢迎光临词")
    max_upload_num: int = Field(description="最大上传数")
    is_upload_files: bool = Field(description="文档上传")
    is_upload_image: bool = Field(description="图片上传")
    is_show_timed: bool = Field(description="显示耗时")
    is_show_quote: bool = Field(description="显示引用")
    is_show_context: bool = Field(description="显上下文")
    is_web_search: bool = Field(description="联网搜索启用")
    broadcast: Dict[str, Union[str, int, float, list]] = Field(description="语音播报参")
    instruct: List[Dict[str, str]] = Field(description="建议指令")

    class Config:
        json_schema_extra = {
            "example": {
                "code": "pdr44hp7bs0u9hti",
                "logo": "http://0.0.0.0:8200/static/images/default_robot.png",
                "name": "通用智能助手",
                "intro": "通用智能体",
                "welcome_text": "欢迎您的使用",
                "max_upload_num": 1,
                "is_upload_files": True,
                "is_upload_image": True,
                "is_show_timed": True,
                "is_show_quote": True,
                "is_show_context": True,
                "is_web_search": False,
                "broadcast": {
                    "model": "xunfei:x4_lingxiaoxuan_oral",
                    "speed": 0.25,
                    "speed_range": [0.1, 10, 0.1]
                },
                "instruct": [
                    {
                        "name": "帮我写作",
                        "content": "帮我写一首关于春天的诗"
                    }
                ]
            }
        }
